<template>
  <div class="settings-container">
    <el-card>
      <el-tabs v-model="activeName">
        <el-tab-pane label="角色管理" name="first">
          <div>
            <el-button
              type="primary"
              @click.native="addSole"
            >+添加角色</el-button>
            <!-- 表格 -->
            <el-table
              :default-sort="{ prop: 'name', order: 'descending' }"
              :data="tableData"
              border
              style="width: 100%; margin: 15px 0"
            >
              <div class="row123">
                <el-table-column fixed type="index" label="序号" width="150" />
                <el-table-column prop="name" sortable label="角色名" />
                <el-table-column prop="description" sortable label="描述" />
                <el-table-column label="操作">
                  <template slot-scope="scope">
                    <el-button
                      type="success"
                      size="small"
                      @click="handleClick(scope.row)"
                    >分配权限</el-button>
                    <el-button
                      type="primary"
                      size="small"
                      @click.native="editSole(scope.row)"
                    >编辑</el-button>
                    <el-button
                      type="danger"
                      size="small"
                      @click.native="delSole(scope.row.id)"
                    >删除</el-button>
                  </template>
                </el-table-column>
              </div>
            </el-table>

            <!-- 分页 -->
            <el-pagination
              :current-page.sync="paramsObj.page"
              :page-sizes="[1, 3, 5, 7, 10]"
              :page-size="paramsObj.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-tab-pane>

        <!-- 公司信息页面开始 -->
        <el-tab-pane label="公司信息" name="second">
          <el-form ref="form" :model="companyForm" label-width="80px">
            <el-form-item label="企业名称">
              <el-input v-model="companyForm.name" disabled />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="companyForm.companyAddress" disabled />
            </el-form-item>
            <el-form-item label="公司电话">
              <el-input v-model="companyForm.companyPhone" disabled />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="companyForm.mailbox" disabled />
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="companyForm.remarks" disabled />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 添加部门角色弹出框 -->
    <SoleDia
      :is-edit="isEdit"
      :edit-data="editData"
      :dialog-form-visible.sync="dialogFormVisible"
      @success="getRolesList"
    />
    <!-- 添加部门角色弹出框 -->
    <!-- 分配权限的弹层 -->
    <el-dialog
      title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
      :visible.sync="showDialogAssign"
    >
      <assign-permission
        v-if="showDialogAssign"
        :role-id="roleId"
        @close="close"
      />
    </el-dialog>
  </div>
</template>

<script>
import 'animate.css'
import assignPermission from './assignPermission'
import SoleDia from './components/soleDia'
import {
  getCompanyInfoByIdApi,
  getRolesListApi,
  delRolesListApi
} from '@/api/settings'
export default {
  components: { SoleDia, assignPermission },
  data() {
    return {
      roleId: '',
      showDialogAssign: false,
      total: 0,
      isEdit: false,
      editData: {},
      dialogFormVisible: false,
      activeName: 'first',
      companyForm: {
        name: '',
        companyAddress: '',
        companyPhone: '',
        mailbox: '',
        remarks: ''
      },
      tableData: [],
      paramsObj: {
        page: 1,
        pagesize: 10
      }
    }
  },
  created() {
    this.getRolesList() // 获取角色列表数据
    this.getCompanyInfoById() // 获取公司详细信息数据
  },
  methods: {
    handleClick(row) {
      console.log(row, '角色ID')
      this.showDialogAssign = true
      this.roleId = row.id
    },
    close() {
      this.showDialogAssign = false
    },
    delSole(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await delRolesListApi(id)
          this.getRolesList()
          console.log(res)
          this.$notify({
            title: '成功',
            message: '删除角色数据成功!',
            type: 'success'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 编辑角色按钮
    editSole(data) {
      this.isEdit = true
      this.dialogFormVisible = true
      this.editData = data
    },
    // 添加角色按钮
    addSole() {
      this.isEdit = false
      this.dialogFormVisible = true
      this.editData = {
        name: '',
        description: ''
      }
    },
    // 获取公司详细信息数据
    async getCompanyInfoById() {
      const res = await getCompanyInfoByIdApi(
        this.$store.state.user.userInfo.companyId
      )
      this.companyForm = res.data
    },
    // 获取角色列表信息
    async getRolesList() {
      const res = await getRolesListApi(this.paramsObj)
      this.tableData = res.data.rows
      this.total = res.data.total
      console.log()
    },

    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.paramsObj.pagesize = val
      this.getRolesList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.paramsObj.page = val
      this.getRolesList()
    }
  }
}
</script>
<style lang="scss" scoped>
.el-dialog {
  margin-top: 1vh !important;
}
.settings-container {
  .el-form {
    width: 500px;
    margin-left: 100px;
  }
  .el-pagination {
    text-align: right;
  }
}
</style>
